<?php

class MailingLists extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'MailingLists':
	 * @var integer $id
	 * @var string $name
	 * @var integer $userId
	 * @var integer $managerEmail
	 * @var string $create_time
	 * @var string $update_time
	 * @var integer $members
	 * @var string $startDate
	 * @var string $latestDate
	 * @var string $period
	 * @var string $total
	 * @var string $status
	 * @var string $email
	 * @var string $enabled
	 */
	public $members;
	public $startDate;
	public $latestDate;
	public $period;
	public $total;
	public $status;
	public $manager;
	public $email;
	public $enabled;

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'MailingLists';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name, managerEmail', 'required'),
			array('name', 'length', 'max'=>128),
			array('name', 'unique'),
			array('managerEmail', 'email'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'Managers', 'userId'),
			'subscribers' => array(self::HAS_MANY, 'Subscribers', 'mlId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'name' => 'メーリングリスト',
			'userId' => '管理者ID',
			'managerEmail' => '管理者Email',
			'create_time' => '登録日時',
			'update_time' => '更新日時',
			'members'=>'参加人数',
			'startDate'=>'開始日',
			'latestDate'=>'最新投稿日',
			'period'=>'期間[日間]',
			'total'=>'メール総数[通]',
			'status'=>'状況(経過日数)',
			'manager'=>'管理者',
			'email'=>'実行メルアド',
			'enabled'=>'有効/無効',
		);
	}

        /**
         * @return array behaviors
         */
	public function behaviors(){
		return array(
                	'CTimestampBehavior' => array(
				'class' => 'zii.behaviors.CTimestampBehavior',
			),
                	'UserIdBehavior' => array(
                        	'class' => 'application.components.UserIdBehavior',
			),
		);
	}

        public function startDate($name)
	{
	  $file = "/var/spool/mail-list/$name/1";
	  $file2 = "/var/spool/mail-list/$name/1.gz";
	  if (is_file($file)) $this->startDate = filemtime($file);
	  else if (is_file($file2)) $this->startDate = filemtime($file2);
	  else $this->startDate = 0;

	  if ($this->startDate == 0)
	    return '-------';
	  else
	    return date("Y/m/d", $this->startDate);
        }

        public function latestDate($name)
	{
	  $file = "/var/spool/mail-list/$name/INDEX";
	  if (is_file($file)) $this->latestDate = filemtime($file);
	  else $this->latestDate = 0;

	  if ($this->startDate == 0 || $this->latestDate == 0)
	    return '-------';
	  else
	    return date("Y/m/d", $this->latestDate);
        }

	public function period($name)
	{
	  return self::dc(floor($this->latestDate/86400) - floor($this->startDate/86400));
	}

	protected function dc($dates)
	{
	    $q = '';
	    $year = 0;
	    $month = 0;
	    while($dates >= 365) {
	      $year++;
	      $dates -= 365;
	    }
	    while($dates >= 30) {
	      $month++;
	      $dates -= 30;
	    }
	    if ($month >= 12) {
	      $month--;
	      $year++;
	    }
	    if ($year) $q .= ($year . "年");
	    if ($month) $q .= ($month . "か月");
	    $q .= ($dates . "日間");
	    if ($year > 30) $q = "-------";
	    return $q;
	  }

	public function total($name)
	{
	  $file = "/var/spool/mail-list/$name/INDEX";
	  $this->total = 0;
	  if (is_file($file)) {
	    $fl = fopen($file, "r");
	    while (fgets($fl, 1024)) {
	      $this->total++;
	    }
	    fclose($fl);
	  } else $this->total = "-------";

	  return $this->total;
	}

	public function status($name)
	{
	  $diff = floor(mktime()/86400) - floor($this->latestDate/86400);
	  if (is_file("/var/spool/mail-list/$name/ABOLISHED")) {
	    $col = "#999999";
	    $stat = "廃止";
	  } else if ($diff < 30 && $this->total != 0) { // 1か月未満
	    $col = "#aaffaa";
	    $stat = "活発";
	  } else if ($diff < 30 && $this->total == 0) { // 1か月未満
	    $col = "#999999";
	    $stat = "未開始";
	  } else if ($diff < 6*30) { // 6か月未満
	    $col = "#ffffaa";
	    $stat = "停滞";
	  } else if ($diff < 30*12*30) { // 30年未満
	    $col = "#ffaaaa";
	    $stat = "休止";
	  } else {
	    $col = "#999999";
	    $stat = "未開始";
	  }
	  
	  if (($this->startDate == 0 || $this->latestDate == 0) && $col != '#999999')
	    $dates = '-------';
	  else
	    $dates = self::dc($diff);

	  return $stat."   ($dates)";
	}

}